#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Ka3u6y6a'

from selenium import webdriver
import Image
import ImageChops
import os
import settings as set

url = "http://ya.ru/"
img1 = "D:\\chrome.png"
img2 = "D:\\firefox.png"
img3 = "D:\\diff.png"
img4 = "D:\\diff4.png"
img5 = "D:\\browsers.png"
img6 = "D:\\browsers1.png"

def testBrowser(path, browser, url):
    """
    path = path to the project TestCase folder
    Browser = IE, CH, FF
    url = Test URL
    """
    sizePath = os.getcwd()+'\\engine\\size.html'
    winWidth = int(set.getSett('browserwinwidth'))
    winHight = int(set.getSett('browserWinHight'))
    
    if browser == 'IE':
        webdrv = webdriver.Firefox()

        ###Выравниваем окно
        webdrv.get(sizePath)

        select = webdrv.find_element_by_id("win")
        webdrv.set_window_size(winWidth,winHight)
        print select.text
        #Horisontal
        realWW = int(select.text.split('/')[0])
        if realWW < winWidth:
            resultWidth = winWidth + (winWidth - realWW)
            webdrv.set_window_size(resultWidth, winHight)
            select = webdrv.find_element_by_id("win")
            print select.text
        if realWW > winWidth:
            resultWidth = winWidth - (realWW - winWidth)
            webdrv.set_window_size(resultWidth, winHight)
            select = webdrv.find_element_by_id("win")
            print select.text
            #Vertical
        realWH = int(select.text.split('/')[1])
        if realWH < winHight:
            resultHight = winHight + (winHight - realWH)
            webdrv.set_window_size(resultWidth, resultHight)
            select = webdrv.find_element_by_id("win")
            print select.text
        if realWH > winHight:
            resultHight = winHight - (realWH - winHight)
            webdrv.set_window_size(resultWidth, resultHight)
            select = webdrv.find_element_by_id("win")
            print select.text
        ###Выравниваем окно end

        webdrv.save_screenshot(os.path.join(path, 'ie9.png'))
        #webdrv.quit()        


    elif browser == 'CH':
        webdrv = webdriver.Chrome("engine\\chromedriver.exe")

        ###Выравниваем окно
        webdrv.get(sizePath)

        select = webdrv.find_element_by_id("win")
        webdrv.set_window_size(winWidth,winHight)
        print select.text
        #Horisontal
        realWW = int(select.text.split('/')[0])
        if realWW < winWidth:
            resultWidth = winWidth + (winWidth - realWW)
            webdrv.set_window_size(resultWidth, winHight)
            select = webdrv.find_element_by_id("win")
            print select.text
        if realWW > winWidth:
            resultWidth = winWidth - (realWW - winWidth)
            webdrv.set_window_size(resultWidth, winHight)
            select = webdrv.find_element_by_id("win")
            print select.text
            #Vertical
        realWH = int(select.text.split('/')[1])
        if realWH < winHight:
            resultHight = winHight + (winHight - realWH)
            webdrv.set_window_size(resultWidth, resultHight)
            select = webdrv.find_element_by_id("win")
            print select.text
        if realWH > winHight:
            resultHight = winHight - (realWH - winHight)
            webdrv.set_window_size(resultWidth, resultHight)
            select = webdrv.find_element_by_id("win")
            print select.text
        ###Выравниваем окно end
            
        webdrv.get(url)
        webdrv.save_screenshot(os.path.join(path, 'chrome.png'))
        webdrv.quit()
        return img1
    
    elif browser == 'FF':
        webdrv = webdriver.Firefox()
        
        ###Выравниваем окно
        webdrv.get(sizePath)

        select = webdrv.find_element_by_id("win")
        webdrv.set_window_size(winWidth,winHight)
        print select.text
        #Horisontal
        realWW = int(select.text.split('/')[0])
        if realWW < winWidth:
            resultWidth = winWidth + (winWidth - realWW)
            webdrv.set_window_size(resultWidth, winHight)
            select = webdrv.find_element_by_id("win")
            print select.text
        if realWW > winWidth:
            resultWidth = winWidth - (realWW - winWidth)
            webdrv.set_window_size(resultWidth, winHight)
            select = webdrv.find_element_by_id("win")
            print select.text
        #Vertical
        realWH = int(select.text.split('/')[1])
        if realWH < winHight:
            resultHight = winHight + (winHight - realWH)
            webdrv.set_window_size(resultWidth, resultHight)
            select = webdrv.find_element_by_id("win")
            print select.text
        if realWH > winHight:
            resultHight = winHight - (realWH - winHight)
            webdrv.set_window_size(resultWidth, resultHight)
            select = webdrv.find_element_by_id("win")
            print select.text
        ###Выравниваем окно end
            
        webdrv.get(url)
        webdrv.save_screenshot(os.path.join(path, 'ff.png'))
        webdrv.quit()
        return img2
    

def compareImages(img1, img2):
    """
    Compare Images: img1, img2
    """
    settings = set.getSett('diffcolor').split('.')

    im1 = Image.open(img1)
    im2 = Image.open(img2)

    img = ImageChops.difference(im1.convert('RGB'), im2.convert('RGB'))
    #diff.save(img4)##### 3

    #img = Image.open('img.png')
    img = img.convert('L')
    img = img.convert("RGBA")
    

    pixdata = img.load()

    for y in xrange(img.size[1]):
        for x in xrange(img.size[0]):
            if pixdata[x, y] == (0, 0, 0, 255):
                pixdata[x, y] = (0, 0, 0, 0)
            else: pixdata[x, y] = (int(settings[0]),
                                   int(settings[1]),
                                   int(settings[2]),
                                   int(settings[3]))
                
    img.save(img4, 'png')#Можно кильнуть
    return img


#set1 = {'r': 100, 'g': 31, 'b': 31, 'transp': 200, 'sens': 1}
#compareImages(img1, img2, set1)
